//
//  ClassifyCollectionViewCell.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "ClassifyCollectionViewCell.h"
#import "DetailsCollectionViewCell.h"
#import "WXCNetWorkTool.h"
#import "DetailsSecondCollectionViewCell.h"
#import "ClassModel.h"
#import "UIImageView+WebCache.h"
#import "HeaderCollectionReusableView.h"
#import "NetAddress.h"
@implementation ClassifyCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self getHandle];
       self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.flowLayout.minimumInteritemSpacing = 10;
        self.flowLayout.minimumLineSpacing = 10;
        self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:self.flowLayout];
        [self addSubview:_collectionView];
        
        self.collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[DetailsCollectionViewCell class] forCellWithReuseIdentifier:@"DetailsCollectionViewCell"];
        [_collectionView registerClass:[DetailsSecondCollectionViewCell class] forCellWithReuseIdentifier:@"DetailsSecondCollectionViewCell"];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        
        

        
        
        
    }
    
    return self;
    
}

#pragma mark - 解析数据
- (void)getHandle {
    
    [WXCNetWorkTool getWithUrl:@"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=android&picVersion=11&scale=2" parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
        NSArray *listArr = [result objectForKey:@"list"];
        self.listArr = [NSMutableArray array];
        self.secListArr = [NSMutableArray array];
        
        NSMutableArray *secArr = [NSMutableArray array];
        for (int i = 5; i < listArr.count; i++) {
            [secArr addObject:listArr[i]];
            
        }
        
        for (NSDictionary *dic in listArr) {
                ClassModel *classModel = [[ClassModel alloc] init];
          [classModel setValuesForKeysWithDictionary:dic];

            
            [self.listArr addObject:classModel];
            }
    
        for (NSDictionary *dic in secArr) {
              ClassModel *classModel = [[ClassModel alloc] init];
                    [classModel setValuesForKeysWithDictionary:dic];
                    [self.secListArr addObject:classModel];
            }
        [_collectionView reloadData];
        
    } fail:^(NSError *error) {
        
    }];
    
    
}

#pragma mark - 创建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.indexPath = indexPath;

    if (indexPath.section == 0) {
        
        DetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailsCollectionViewCell" forIndexPath:indexPath];
//        cell.backgroundColor = [UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1];
            NSString *str = [[self.listArr objectAtIndex:0] coverPath];
            
            [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:str]];
            NSLog(@"%@",str);
            
            [cell.imageView2 sd_setImageWithURL:[NSURL URLWithString:[self.listArr[1] coverPath]]];
            [cell.imageView3 sd_setImageWithURL:[NSURL URLWithString:[self.listArr[2] coverPath]]];
            [cell.imageView4 sd_setImageWithURL:[NSURL URLWithString:[self.listArr[3] coverPath]]];
            [cell.imageView5 sd_setImageWithURL:[NSURL URLWithString:[self.listArr[4] coverPath]]];
        cell.block = ^(NSString *string) {
//            MoreViewController *moreVC = [[MoreViewController alloc] init];
//            moreVC.categoryId = string;
//            self.
            self.newBlock(string);
            NSLog(@"🐂%@", string);

        };
        

        
        
        
        
        
        return cell;
    } else {
        DetailsSecondCollectionViewCell *secCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailsSecondCollectionViewCell" forIndexPath:indexPath];
        

        
        [secCell.imageView sd_setImageWithURL:[NSURL URLWithString:[self.secListArr[indexPath.item] coverPath]]];
        secCell.label.text = [self.secListArr[indexPath.item] title];

        
        return secCell;
    }
    
    return nil;
}

#pragma mark - item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }

    
    else {
        return self.secListArr.count;
    }
   
}

#pragma mark - section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}


#pragma mark - collectionHeader
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        HeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        
        return header;
    }
    
    return nil;
    
    
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        
        return  CGSizeMake(self.width , 150);
    } else {
       
        return  CGSizeMake(UIScreen_WIDTH/2-10, 40);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        self.newBlock([self.secListArr[indexPath.item] id]);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 10, 0);
}





@end
